/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javacademic.pessoa;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import java.util.ArrayList;
import javacademic.Config;

/**
 *
 * @author leidson
 */
public class ListarPessoaServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ArrayList pessoasLista = new ArrayList();
        try {
            String nome = request.getParameter("nome");
            if (nome == null || nome.trim().length() <= 0) {
                nome = "%";
            } else {
                nome = "%" + nome + "%";
            }
            Class.forName("org.postgresql.Driver").newInstance();
            Connection connection = DriverManager.getConnection(Config.db_adress,Config.db_user,Config.db_password);
            Statement statement = connection.createStatement();
            String sql = "select codigo, nome, endereco, email from pessoa where upper(nome) like '" + nome.toUpperCase() + "' order by codigo asc";
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                Pessoa p = new Pessoa();
                p.setCodigo(resultSet.getInt("codigo"));
                p.setNome(resultSet.getString("nome"));
                p.setEmail(resultSet.getString("email"));
                p.setEndereco(resultSet.getString("endereco"));
                pessoasLista.add(p);
            }
            statement.close();
            connection.close();
        } catch (SQLException ex) {
            Logger.getLogger(ListarPessoaServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ListarPessoaServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ListarPessoaServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ListarPessoaServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        request.setAttribute("pessoasLista", pessoasLista);

        RequestDispatcher requestDispatcher = request.getRequestDispatcher("WEB-INF/jsp/pessoa/listar.jsp");
        requestDispatcher.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="Métodos HttpServlet. Clique no sinal de + à esquerda para editar o código.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
